//
//  NNGAnalysisViewController.m
//  nanningOne
//
//  Created by kabu on 2018/9/27.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NNGAnalysisViewController.h"
#import "NNGHistoryAction.h"
#import <NGC/NGC.h>
#import <NGC/NGCWebViewDelegate.h>
//#import "NGCApis.h"
//#import "NGCWebViewDelegate.h"
//#import "NGCWebViewController.h"
//#import "NGCWebView.h"
//#import "NGCApisManager.h"
//#import "NGCBridge.h"
@interface NNGAnalysisViewController ()
@property (nonatomic, strong) NSString *paramMonth;
@property (strong, nonatomic) NGCWebViewController *web_VC;
@end

@implementation NNGAnalysisViewController

- (void)viewWillAppear:(BOOL)animated{
    [SVProgressHUD show];
}
-(void)viewDidAppear:(BOOL)animated{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         [SVProgressHUD dismiss];
    });
   
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    if ([self.flag isEqualToString:@"stat"]) {
        [self.navigationItem setItemWithTitle:@"工作统计" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    }else{
        [self.navigationItem setItemWithTitle:@"营商服务分析" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    }
    [self changeUserAgent];
    if (self.month) {
        self.paramMonth=[NSString stringWithFormat:@"%0ld",self.month];
    }
    [self loadHomeWeb];
    // Do any additional setup after loading the view.
}
- (void)setWebUrl:(NSString *)webUrl{
    _webUrl=webUrl;
    if (self.web_VC.webView) {
        [self.web_VC.webView loadHostUrl:webUrl];
    }
}
/**
 更改User-Agent
 */
- (void)changeUserAgent{
    NSString *buildVersion=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *newUserAgent = [userAgent stringByAppendingString:[NSString stringWithFormat:@" youeApp/%@ NGC/1.0",buildVersion]];//自定义需要拼接的字符串
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
- (void)loadHomeWeb{
    
    if (!self.web_VC) {
        NSString *url=[self.webUrl hasPrefix:@"http"]?self.webUrl:@"http://cdyoue.com.cn:18180/nanning/businessServiceAnalysis/serviceAnalysis.html";
        if (self.paramMonth) {
            if ([url hasSuffix:@"?"]) {
                url=[NSString stringWithFormat:@"%@&month=%@",url,self.paramMonth];
            }else{
                url=[NSString stringWithFormat:@"%@?month=%@",url,self.paramMonth];
            }
        }
        self.web_VC=[[NGCApisManager shareInstance] createWebViewControllerWithUrl:url];
        [self.view addSubview:self.web_VC.view];
        [self.web_VC.view setFrame:self.view.bounds];
        [self addChildViewController:self.web_VC];
        [[NGCApisManager shareInstance] registerApis:[[NNGHistoryAction alloc] init]
                                         withWebView:self.web_VC.webView withParam:@{NGCCurrentVC:self}];
    }else{
        [self.web_VC.webView reload];
    }
    
    
}

@end
